package com.example.project.projectspringcloud.controller.front.user;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.controller.BaseController;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.RespBody;
import com.example.project.projectspringcloud.entity.discountcoupon.DiscountCoupon;
import com.example.project.projectspringcloud.entity.user.User;
import com.example.project.projectspringcloud.service.discountcoupon.DiscountCouponService;
import com.example.project.projectspringcloud.service.user.UserService;
import com.example.project.projectspringcloud.utils.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Api(value = "用户端优惠券Controller")
@RequestMapping(value = "/personal/discountcoupon")
@Controller
public class DiscountCouponController extends BaseController {

    @Autowired
    private DiscountCouponService discountCouponService;

    @Autowired
    private UserService userService;

    @ApiOperation(value = "优惠券列表", notes = "返回优惠券列表")
    @GetMapping(value = {"", "/list"})
    public String list(DiscountCoupon discountCoupon, Page page, Model model) {
        discountCoupon.setOwner(super.getCurrentUser().getId());
        discountCoupon.setStatus(1);
        List<DiscountCoupon> discountCoupons = discountCouponService.list(page, discountCoupon);
        model.addAttribute("discountCoupons", discountCoupons);
        model.addAttribute("discountCoupon", discountCoupon);
        return isMobile() ? "m/content/discount/discount-coupon-list" : "content/discount/discount-coupon-list";
    }

    @ApiOperation(value = "转赠优惠券", notes = "跳转到转赠优惠券页面")
    @GetMapping(value = "/present")
    public String present(@RequestParam("id") Integer id, Model model) {
        model.addAttribute("id", id);
        return "m/content/discount/discount-coupon-present";
    }

    @ApiOperation(value = "API转赠优惠券", notes = "用户转赠优惠券")
    @PostMapping(value = "/api/present")
    @ResponseBody
    public RespBody present(@ApiParam(value = "用户持有优惠券ID") Integer id,
                            @ApiParam(value = "转赠目标用户手机") String target) throws Exception {
        // 查找目标是否存在
        User user = new User();
        user.setMobile(target);
        user = userService.get(user);
        if (Optional.ofNullable(user).isPresent()) {
            // 查看当前用户是否持有该券
            DiscountCoupon discountCoupon = new DiscountCoupon();
            User currentUser = WebUtil.getCurrentUser();
            discountCoupon.setId(id);
            discountCoupon.setOwner(currentUser.getId());
            discountCoupon.setStatus(1);

            discountCoupon = discountCouponService.get(discountCoupon);
            if (discountCoupon != null) {
                discountCoupon.setOriginalOwner(discountCoupon.getOwner());
                discountCoupon.setOwner(user.getId());
                discountCouponService.save(discountCoupon);
            } else {
                return RespBody.error(translateI18N(Constants.Tip.DISCOUNT_COUPON_NOT_EXIST));
            }
        } else {
            return RespBody.error(translateI18N(Constants.Tip.ACCOUNT_MOBILE_NOT_EXIST));
        }

        return RespBody.ok(true);
    }
}
